<cfparam name = "dir" default="/">
<cfparam name = "searchtxt" default="">
<cfparam name = "aindex" default="1">
<cfoutput><script type="text/javascript" src="#request.absolutepath#js/tinymce/tiny_mce_popup.js"></script></cfoutput>
<script>tinyMCEPopup.onInit.add(FileBrowserDialogue.init, FileBrowserDialogue);</script>
<style>
body {background: none; margin: 0px; padding: 0px;}
</style>

<!---if image is uploaded them select uploaded image and close--->
<cfif isdefined('url.imagefilename')>
	<cfset imageurl = "#request.absolutepath#images#dir##imagefilename#">
	<cfoutput>
    <script>
    FileBrowserDialogue.getlink('#imageurl#');
	</script>
    </cfoutput>
</cfif>

<cfset charlist = '$,1,2,3,4,5,6,7,8,9,0'>
<cfset alphalist = "1,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z">

<cfset usepe = 'Yes'>
<cfset fullpath = "#request.basepath#images#dir#">

<cfdirectory  action = "List" directory = "#fullpath#" name = "qAllFiles" type="file" sort="name ASC" listinfo="name">
<cfdirectory  action = "List" directory = "#fullpath#" name = "qfolders" type="dir" sort="name ASC" listinfo="name">

<cfif len(trim(searchtxt)) IS 0>
    <cfquery name="qryFileNames" dbtype="query">
    SELECT * FROM qAllFiles
    <cfif NOT aindex IS '1'>WHERE name LIKE ('#aindex#%') OR name LIKE ('#lcase(aindex)#%')</cfif>
    <cfif aindex IS '1'>
        WHERE name LIKE ('$%')
        <cfloop list="#charlist#" index="c">
            OR name LIKE ('#c#%') 
        </cfloop>
    </cfif>
    </cfquery>
<cfelse>
    <cfquery name="qryFileNames" dbtype="query">
    SELECT * FROM qAllFiles
    WHERE name LIKE ('%#searchtxt#%')
    </cfquery>
</cfif>

<!---create the alphaindex--->
<cfsavecontent variable="alphaoutput">
<cfloop list="#alphalist#" index="ltr" delimiters=",">

    <cfquery name="qcount" dbtype="query">
    SELECT count(*) AS indextotal FROM qAllFiles
    <cfif NOT ltr IS '1'>WHERE name LIKE ('#ltr#%') OR name LIKE ('#lcase(ltr)#%')</cfif>
    <cfif ltr IS '1'>
        WHERE name LIKE ('$%')
        <cfloop list="#charlist#" index="c">
            OR name LIKE ('#c#%') 
        </cfloop>
    </cfif>
    </cfquery>

<cfif qcount.indextotal GT 0>
	<cfoutput><span id="alphaindex" class="alphabox" onMouseOver="this.className='alphabox_over';" onMouseOut="this.className='alphabox';" onClick="window.location.href = 'browse.cfm?aindex=#ltr#&field_name=#field_name#&win=#win#&type=#type#&dir=#dir#';"><a href = "browse.cfm?aindex=#ltr#&field_name=#field_name#&win=#win#&type=#type#&dir=#dir#">#ltr#</a></span></cfoutput>
<cfelse>
	<cfoutput><span id="alphaindex_disabled" class="alphabox"><a href = "##">#ltr#</a></span></cfoutput>
</cfif>
</cfloop>
</cfsavecontent>
<cfset showfolders = 1>
<div id="component_menus">
  <cfinvoke component="cfcs.common.buttons" method="show" width="125" btext="Upload an Image" tooltip="Upload an image and select it" jscript="collapse('searchform'); simpleexpandcollapse('fileupload');" img="upload.png" />
  <cfinvoke component="cfcs.common.buttons" method="show" width="125" btext="Search For Image" tooltip="Search for a particular image" jscript="collapse('fileupload'); simpleexpandcollapse('searchform');" img="view.png" />
  <div class="clear"></div>
</div>
<div style="display: none; border: 1px solid #003366; padding: 20px;" id="fileupload">
<cfoutput>
<script type="text/javascript">
$(document).ready(function() {
  $('##file_upload').uploadify({
	'uploader'  : '#request.absolutepath#js/uploadify/uploadify.swf',
	'script'    : '#request.absoluteadminpath#upload/upload.cfm',
	'cancelImg' : '#request.absolutepath#images/defaults/cancel.png',
	'folder'    : '#request.absolutepath#images#dir#',
	'scriptData'  : {'filepath':'#request.basepath#images/','dir':'#dir#'},	
	'auto'      : true,
	'multi'		: true,
	'buttonText': 'Select Image',	
	'queueSizeLimit' : 20,
	'sizeLimit' : 183500800,
	'onComplete' : function(event, queueID, fileObj, response, data) {
	var relocateto = 'browse.cfm?c=imagelibrary&aindex=#aindex#&searchtxt=#searchtxt#&field_name=#field_name#&win=#win#&type=#type#&dir=#dir#&imagefilename='+fileObj.name;
	window.location.href = relocateto;
	}	
  });
});
</script>
</cfoutput>
 <input id="file_upload" name="file_upload" type="file" /> 
</div>
<div style="display: none; border: 1px solid #003366; padding: 20px;" id="searchform">
	<cfoutput>
    <form method="POST" action="browse.cfm?field_name=#field_name#&win=#win#&type=#type#&dir=#dir#" name="searchform">
    Find:&nbsp;<input type="text" size="20" value="#searchtxt#" name="searchtxt" />
    <input type="submit" name="submitbtn" value="Go" id="abutton" />
    </form> 
    </cfoutput>  
</div>
<table width="835" border="0" cellpadding="4" cellspacing="0">
<tr>
	<td colspan="4">
	<cfif NOT dir IS '/'>
        <!---figure out which directory is up one from this one--->
        <cfset PathLength = listlen(dir, "/")>
        <cfset UpdirName = ListDeleteAt(dir, PathLength, '/')>
        <cfif UpdirName IS ''>
          <cfset UpdirName = '/'>
        </cfif>
        <cfif NOT right(UpdirName, 1) IS '/'>
          <cfset updirName = "#updirName#/">
        </cfif>
        <cfoutput><img src="#request.absoluteadminpath#images/folder_up.png" align="absmiddle" width="24"> <a href = "browse.cfm?aindex=1&field_name=#field_name#&win=#win#&type=#type#&dir=#UpdirName#">[Parent Folder]</a></cfoutput>
      </cfif>    
  </td>
</tr>
  <cfoutput>
  <tr>
    <td id="columnheaders" colspan="3" style="cursor: pointer;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');">Folders [Current Path: #dir#]</td>
    <td id="columnheaders" align="right" style="cursor: pointer;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');"><img src="#request.absolutepath#images/defaults/collapse.gif" border="0" id="expandcollapseimg" /></td>
  </tr>
  </cfoutput>
  <tr>
    <td colspan="4">
    <cfif qfolders.recordcount GT 0>
    <div id="folders" <cfif showfolders IS 0>style="display: none;"</cfif>>
        <table width="100%" cellpadding="4" cellspacing="0">
          <cfoutput Query = "qfolders">
            <cfset ImageName = 'folder.png'>
            <tr class="menu_row" onMouseOver="this.className = 'menu_row_over2';" onMouseOut="this.className = 'menu_row';">
              <td width="90%" class="menu_cell" onclick="window.location.href = 'browse.cfm?aindex=1&field_name=#field_name#&win=#win#&type=#type#&dir=#dir##Name#/';"><img src="images/#ImageName#" align="absmiddle"> <a href = "browse.cfm?aindex=1&field_name=#field_name#&win=#win#&type=#type#&dir=#dir##Name#/">#Name#</a></td>
              <td width="10%" class="menu_cell"></td>
            </tr>
          </cfoutput>
        </table>
      </div>
        <cfoutput>
          <cfif showfolders IS 0><div id="expandcollapse_msg" style="display: block;" onclick="expandcollapse('folders','expandcollapseimg','#request.absolutepath#images/defaults/');">Click to Expand</div></cfif>
        </cfoutput>
      <cfelse>
      	<div id="messages_error">No Folders Found.</div>
	  </cfif>
    </td>
  </tr>
<tr>
	<td colspan="4" style="border-bottom: 4px solid black;">
    	<table width="100%" cellpadding="0" cellspacing="0">
        	<tr>
            	<td width="70%">
					<cfoutput>#alphaoutput#</cfoutput>
			  </td>             
	    </tr>
    </table>
   </td>
</tr>
    
<cfif NOT Dir IS '/'>
<!---figure out which directory is up one from this one--->
	<cfset PathLength = listlen(dir, "/")>
	<cfset UpDirName = ListDeleteAt(dir, PathLength, '/')>
	<cfif UpDirName IS ''><cfset UpDirName = '/'></cfif>
</cfif>
<!---If it's not the main folder then display the list of folders in this directory--->
<cfoutput Query = "qfolders">
  <cfset ImageName = 'folder.gif'>
</cfoutput>
<cfset dcount = 0>
<cfset colcount = 0>
<tr>

<!---Show the images--->
<cfloop query = "qryFileNames">

<cfoutput>
<!---Figure out what action to use on this file from it's file type--->
<cfset theaction='browse'>
<cfset theicon='document_filetype.gif'>
<cfset thetype='none'>

<cfif Right(name, 4) IS '.gif' OR right(name, 4) IS '.jpg' OR right(name, 4) IS '.png' or right(name,4) IS 'jpeg'>
	<cfset theaction='viewimage'>
	<cfset theicon='image_filetype.gif'>
	<cfset thetype='Image'>
</cfif>

<!---Display the list of files--->
	<cfif thetype IS 'Image'>
	    <cfset colcount = colcount + 1>
		<cfset dCount = dCount + 1>
		<td height="150" width="25%" valign="middle" align="center" class="image_out" onMouseOver="this.className = 'image_over';" onMouseOut="this.className = 'image_out';">
        <table width="100%" cellpadding="4" cellspacing="0" align="center">
        <tr>
        <td height="115" align="center">
        
        <cfset imageurl = "#request.absolutepath#images#dir##name#">
        <cfset imageurl = replace(imageurl, "//", "/", "ALL")>
        
        <cfif usepe IS 'Yes'>
        	<cfset onclickevent = "FileBrowserDialogue.getlink('#imageurl#');">
        <cfelse>
        	<cfset onclickevent = "FileBrowserDialogue.getlink('#imageurl#');">
        </cfif>
		
		<cfif fileexists('#request.basepath#images#dir#thumbs/#name#')>
        	<cfset imgsrc = "#request.absolutepath#images#dir#thumbs/#name#">
        <cfelse>
        	<cfset imgsrc = "#request.absolutepath#images#dir##name#">
        </cfif>        
        <div align="center" style="height: 150px; overflow: hidden; width: 175px; cursor: pointer;" onClick="#onclickevent#">
        <center><img src = "#imgsrc#" style="max-width: 175px;" border="0" alt="#name# - click to select" title="#name# - click to select" onClick="#onclickevent#"></center><br />
        </div>
        <center>
       	<span onClick="#onclickevent#" style="cursor:pointer;">#name#</span></center>
        </a>
        </td>
        </tr>
        </table>        </td>
	<cfif colcount GT 3>
    </tr><tr><cfset colcount = 0>
    </cfif>
	</cfif>
</cfoutput>
</cfloop>
</table>
